package com.a_b.back_end.util.DAO.Impl;

import com.a_b.back_end.base.entity.Classes;
import com.a_b.back_end.util.DAO.classesDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author agrinJPG
 * @date 2021/5/3 22:57
 * @Description Classes Dao 实现类
 */
@Repository
public class classesDaoJdbcTemplateImpl implements classesDao {
    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public int add(Classes classes) {
        String sql = "insert into classes " +
                "values(:txId,:clas,:time)";
        Map<String,Object> param = new HashMap<>();
        param.put("txId",classes.getTxId());
        param.put("clas",classes.getClas());
        param.put("time",classes.getTime());

        return (int)jdbcTemplate.update(sql,param);
    }

    @Override
    public int update(Classes classes) {
        String sql = "update classes " +
                "set clas=:clas,time=:time " +
                "where txId=:txId";
        Map<String,Object> param = new HashMap<>();
        param.put("clas",classes.getClas());
        param.put("time",classes.getTime());
        param.put("txId",classes.getTxId());

        return (int)jdbcTemplate.update(sql,param);
    }

    @Override
    public int delete(String txId) {
        String sql = "delete from classes " +
                "where txId =:txId";
        Map<String,Object> param = new HashMap<>();
        param.put("txId",txId);

        return (int)jdbcTemplate.update(sql,param);
    }

    @Override
    public Classes findClasses(String txId) {
        String sql = "select * from classes " +
                "where txId =:txId";
        Map<String,Object> param = new HashMap<>();
        param.put("txId",txId);
        RowMapper<Classes> res = new RowMapper<Classes>() {
            @Override
            public Classes mapRow(ResultSet resultSet, int i) throws SQLException {
                Classes classes = new Classes();
                classes.setTxId(resultSet.getString("txId"));
                classes.setTime(resultSet.getInt("time"));
                classes.setClas(resultSet.getString("clas"));
                return classes;
            }
        };
        List<Classes> results = jdbcTemplate.query(sql,param,res);
        Classes result = results.get(0);
        return result;
    }

    @Override
    public List<Classes> findClassesList() {
        return null;
    }
}
